package com.ccx.coachsite.ui.share;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;

import com.ccx.coachsite.R;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.login.LoginActivity;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.own.OwerViewKey;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.util.ContactsUtil;
import com.ccx.coachsite.util.StringUtil;

/**
 * 手机号码View Tab
 */
public class MobilePhoneView extends CommonAppView {

	private EditText edtPhone1;
	private EditText edtPhone2;
	private EditText edtPhone3;
	private View btnSend; 
	private Context mContext;
	private List<String> inputs;
	private OpenSendListener mOpenSend;
	private View btnBack;
	
	public MobilePhoneView(Context paramContext) {
		super(paramContext);
		mContext = paramContext;
		addView(R.layout.share_mobile_phone);
		edtPhone1 = (EditText)findViewById(R.id.tuijian_edit_phone1);
		edtPhone2 = (EditText)findViewById(R.id.tuijian_edit_phone2);
		edtPhone3 = (EditText)findViewById(R.id.tuijian_edit_phone3);
		btnSend = findViewById(R.id.share_button_phone_send);
		btnSend.setOnClickListener(new OnSendListener());
		
		btnBack = findViewById(R.id.share_button_phone_back);
		btnBack.setOnClickListener(new OnBackListener());
		
		inputs = new ArrayList<String>();
	}
	
	public void setOpenSendListener(OpenSendListener openSend){
		mOpenSend = openSend;
	}
	
	private boolean checkInput()
	{
		inputs.clear();
		CommonViewUtils.hideKeyboard((Activity)mContext);
		Animation shake = AnimationUtils.loadAnimation(mContext,
				R.anim.shake);
		String inphone1 = edtPhone1.getText().toString().trim();
		String inphone2 = edtPhone2.getText().toString().trim();
		String inphone3 = edtPhone3.getText().toString().trim();
		
		if(StringUtil.isEmpty(inphone1) && StringUtil.isEmpty(inphone2) && StringUtil.isEmpty(inphone3))
		{
			CommonViewUtils.showErrorByEditText(edtPhone1,
					"至少输入一个天翼手机号码", shake);
			return false;
		}
		
		if(!StringUtil.isEmpty(inphone1) && (inphone1.length() < 11 || !ContactsUtil.checkContact(inphone1))) {
				CommonViewUtils.showErrorByEditText(edtPhone1,
						"请输入正确的天翼手机号码", shake);
				return false;
		}
		
		if(!StringUtil.isEmpty(inphone2) && (inphone2.length() < 11 || !ContactsUtil.checkContact(inphone2))) {
			CommonViewUtils.showErrorByEditText(edtPhone2,
					"请输入正确的天翼手机号码", shake);
			return false;
		}

		if(!StringUtil.isEmpty(inphone3) && (inphone3.length() < 11 || !ContactsUtil.checkContact(inphone3))) {
			CommonViewUtils.showErrorByEditText(edtPhone3,
					"请输入正确的天翼手机号码", shake);
			return false;
		}
		
		if(!StringUtil.isEmpty(inphone1))
			inputs.add(inphone1);
		if(!StringUtil.isEmpty(inphone2))
			inputs.add(inphone2);
		if(!StringUtil.isEmpty(inphone3))
			inputs.add(inphone3);
		
		return true;
	}
	
	private void send()
	{
		if (mOpenSend!=null)
			mOpenSend.sendSMS(1, OpenShareDetail.getShareUserIds(inputs));
	}
	
	class OnBackListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Activity act = (Activity)mContext;
			if(act != null)
				act.onBackPressed();
		}
	}
	
	class OnSendListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(checkInput())
			{
				send();
			}
		}
	}

}
